package com.victor.service.impl;

import java.util.List;
import java.util.Map;

import javax.management.RuntimeErrorException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.victor.dao.UserDao;
import com.victor.entity.user.LoginQuestion;
import com.victor.entity.user.User;
import com.victor.entity.user.UserBankcard;
import com.victor.service.UserService;
import com.victor.utils.LoginUtil;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Value("${openkey}")
	private String openkey;

	@Value("${appid}")
	private String appid;

	@Value("${sig}")
	private String sig;

	@Value("${pf}")
	private String pf;

	@Override
	public User findByOpenid(String openId) {
		return userDao.wechart(openId);
	}

	@Override
	public boolean insert(User user) {
		return userDao.insertSelective(user);
	}

	@Override
	public User qq(String qqopenid) {
		User user = userDao.qq(qqopenid);
		if (null == user || user.getId() == null) {

			// 新用户 创建账号
			Map<String, String> info = LoginUtil.getQQInfo(qqopenid, openkey, appid, sig, pf);
			if (info.get("ret").equals("0")) {
				user = new User();
				user.setNickname(info.get("nickname"));
				user.setHeadphoto(info.get("figureurl"));
				user.setQqopenid(qqopenid);
			} else {
				return null;
			}
			if (!userDao.insertSelective(user)) {
				throw new RuntimeErrorException(null, "保存用户信息失败");
			}
		}

		return user;
	}

//	邮箱登录
	@Override
	public User mailLogin(String mailbox, String password) {
		return userDao.mailLogin(mailbox, password);
	}

	@Override
	public User selByPhone(String mobile) {
		return userDao.selByPhone(mobile);
	}

	@Override
	public User selByAccount(String account) {
		return userDao.selByAccount(account);
	}

	@Override
	public User selByMail(String mail) {
		return userDao.selByMail(mail);
	}

	@Override
	public boolean updateUser(User user) {
		return userDao.updateByPrimaryKeySelective(user);
	}

	@Override
	public User selById(Integer id) {
		return userDao.selById(id);
	}

	@Override
	public List<UserBankcard> selBankCard(Integer id) {
		return userDao.selBankCardByUid(id);
	}

	@Override
	public List<LoginQuestion> selLoginQuestion() {
		return userDao.selLoginQuestion();
	}

//	app账号三号登录
	@Override
	public User appLoign(String account_mobile_mail, String password) {
		return userDao.appLoign(account_mobile_mail, password);
	}

//	忘记密码
	@Override
	public User getUserByAccount_mobile_mail(String account_mobile_mail) {
		return userDao.getUserByAccount_mobile_mail(account_mobile_mail);
	}

//	验证该userid是否存在这张银行卡
	@Override
	public int getBankcardYoN(Integer userid, String bankcard, String joinbank) {
		return userDao.getBankcardYoN(userid, bankcard, joinbank) != null ? 1 : 0;
	}

	// 添加银行卡信息
	@Override
	public int insUserBankCard(UserBankcard userBankcard) {
		return userDao.insUserBankCard(userBankcard);
	}

}
